﻿using System;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using Framework.SqlDataAccess.Controller;
using Kaysite.Controller;
using Kaysite.Manager;
using Kaysite.Model;

public partial class Controls_AdminControls_ServiceList : BaseListControl<ServiceGroupController, ServiceGroup>
{
    public override ASPxGridView GridView
    {
        get { return gridList; }
    }

    public override ASPxButton AddButton
    {
        get { return btnAddServiceGroup; }
    }

    public override string GetEditPopupTitle(bool isEdit = false)
    {
        return "групу";
    }

    protected void btnAddService_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("{0}?view=ServiceEdit&ServiceGroupID={1}",
            Request.Path,
            gridList.FocusedRowIndex != -1 ? gridList.GetRowValues(gridList.FocusedRowIndex, "ID") : 0));
    }

    protected void gridServices_BeforePerformDataSelect(object sender, EventArgs e)
    {
        var grid = sender as ASPxGridView;
        if (grid==null)
            return;
        var manager = CreateManager<ServiceManager>();
        manager.Filter.AddCondition("ServiceGroupID", eOperationType.Equal, grid.GetMasterRowKeyValue());
        grid.DataSource = manager.GetAll("ServiceName");
    }

    protected void gridServices_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == GridViewRowType.Data)
        {
            int visibleIndex = e.VisibleIndex;
            var grid = sender as ASPxGridView;
            GridButton_SetEventClick(visibleIndex, "btnDelete", GetDeleteScript(visibleIndex, grid), grid);
            GridButton_SetEventClick(e.VisibleIndex,
                "btnEditService",
                string.Format("window.location = '{0}?view=ServiceEdit&ID={1}';",
                    Request.Path,
                    e.KeyValue), grid);
        }
    }

    protected void gridServices_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        try
        {
            var grid = (ASPxGridView)sender;
            var id = (int)e.Values[grid.KeyFieldName];
            CreateManager<ServiceManager>().Delete(id);
            grid.DataBind();
        }
        finally
        {
            e.Cancel = true;
        }
    }
}